package org.user.dao;

import java.sql.SQLException;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.user.pojo.User;

public class UserDaoImpl extends HibernateDaoSupport implements UserDao{

	public User get(final String name) {
		List list = getHibernateTemplate()
        .executeFind(new HibernateCallback() 
        {
        	public Object doInHibernate(Session arg0)throws HibernateException, SQLException 
        	{
        		Criteria cri = arg0.createCriteria(User.class);
        		cri.add(Restrictions.eq("name", name));;
        		List result = cri.list();
				return result;
			}
		});
		if(list.size()==0)
			return null;
		else
			return (User)list.get(0);
	}
	
	public static void main (String []args)
	{
		ApplicationContext ctx = new FileSystemXmlApplicationContext("/WebRoot/WEB-INF/applicationContext.xml");
		UserDaoImpl daoImpl = (UserDaoImpl) ctx.getBean("userDao");
		System.out.println(daoImpl.get("ak").getName());
	}
}
